<template>
  <CpNavBar right-text="注册" title="登录" @click-right="$router.push('/register')"></CpNavBar>
  <div class="login">
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
      <a href="javascript:;">
        <span @click="isPass = !isPass">{{ !isPass ? '密码登录' : '短信验证码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <van-form autocomplete="off" @submit="login">
      <van-field v-model="mobile" placeholder="请输入手机号" type="tel" name="mobile" :rules="mobileRules"></van-field>
      <van-field v-if="isPass" v-model="password" placeholder="请输入密码" :type="iptType" :rules="passwordRules">
        <template #button>
          <cp-icon @click="showPassword" :name="`login-eye-${show ? 'on' : 'off'}`"></cp-icon>
        </template>
      </van-field>
      <van-field v-else v-model="code" placeholder="短信验证码" :rules="codeRules">
        <template #button>
          <span @click="send">{{ time > 0 ? `${time}s后再次发送` : '发送验证码' }}</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit">登 录</van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import { Toast, type FieldType, type FormInstance } from 'vant'
import { loginByPassword, sendMobileCode, loginByMobile } from '@/api/user'
import { useUserStore } from '@/stores'
import { useRouter, useRoute } from 'vue-router'

const agree = ref(false)
const show = ref(false)
const mobile = ref('13230000100')
const password = ref('abc12345')
const code = ref('')
const isPass = ref(true)
const time = ref(0)
const form = ref<FormInstance>()
const iptType = ref<FieldType>('password')
let timeId: number

const store = useUserStore()
const router = useRouter()
const route = useRoute()

const showPassword = () => {
  show.value = !show.value
  show.value ? (iptType.value = 'text') : (iptType.value = 'password')
}

const login = async () => {
  if (!agree.value) {
    return Toast({
      message: '请勾选我已同意',
      position: 'top'
    })
  }
  // 发送请求登录
  const res = isPass.value
    ? await loginByPassword(mobile.value, password.value)
    : await loginByMobile(mobile.value, code.value)
  // 保存用户信息
  store.setUser(res.data)
  // 如果有回调地址就跳转回调地址 没有就跳转个人中心
  router.push((route.query.returnUrl as string) || '/user')
  Toast.success('登录成功')
}

const send = async () => {
  // 时间大于0则return
  if (time.value > 0) {
    return
  }
  // 校验手机号
  await form.value?.validate('mobile')
  sendMobileCode(mobile.value, 'login')
  Toast.success('发送成功')
  time.value = 60
  // 倒计时
  timeId = window.setInterval(() => {
    time.value--
    if (time.value <= 0) {
      window.clearInterval(timeId)
    }
  }, 1000)
}

// 组件销毁时操作
onMounted(() => {
  // 防止已经登录，但是定时器未关停
  window.clearInterval(timeId)
})
</script>

<style lang="scss" scoped>
.login {
  padding-top: 46px;

  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;

    h3 {
      font-weight: normal;
      font-size: 24px;
    }

    a {
      font-size: 15px;
    }
  }

  .van-form {
    padding: 0 14px;

    .cp-cell {
      height: 52px;
      padding: 14px 16px;
      box-sizing: border-box;
      display: flex;
      align-items: center;

      .van-checkbox {
        a {
          color: var(--cp-primary);
          padding: 0 5px;
        }
      }
    }
  }
}

:deep(.van-field__button) {
  margin-top: 5px;
}
</style>
